<?php
/**
 * Created by PhpStorm.
 * User: 李闯
 * Date: 2020/8/6
 * Time: 17:43
 * Content :我的页 查看我的 订单
 */

namespace app\api\controller;

use app\service\MemberService;
use app\service\OrderService;
use think\Db;

class Mine extends Common
{
    /**
     * [__construct 构造方法]
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年8月4日13:41:15
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();

//        $this->Islogin();
    }
    /**
     * @param array
     * @content 我的页 查看 待付款，
     */
    function getMineorderList(){
        /*
       * 时间 ，订单状态，商品名称，商品规格， 售卖价格  数量  缩略图
       */
        $params = input();
        #条件
        $_where ='';

        if(!empty($params['type']))
        {
            $_where .= "mo.is_delete=2 and  mo.order_status = '".$params['type']."' and mo.member_id = $this->member_id";

        }

        $field='id mo_id,create_time,total_price,order_status';
        // 获取列表
        $data_params = [
            'page' => false,
            'where' => $_where,
            'field'=>$field,
            'mine'=>'mineOrderList',
            'order_by'=>'create_time desc'
        ];

        $ret=OrderService::OrderList($data_params);
        $data = empty($ret['data']) ? [] : $ret['data'];
        return json(apiDataReturn(true, '',$data));
    }
    /**
     * @param array
     * @content 我的页 查看 待发货，运输中 历史订单
     */
    public function getMineOrder()
    {
        /*
         * 时间 ，订单状态，商品名称，商品规格， 售卖价格  数量  缩略图
         */
        $params = input();
        #条件
        $_where ='';
        if(!empty($params['type']))
        {
            $_where .= "mod.is_delete=2 and  mod.order_status = '".$params['type']."' and mo.member_id = $this->member_id";
        }
        $field='mod.id mod_id,mo.id mo_id,
        mod.order_status,mod.bizpro_name,
        mod.bizpro_imgurl,mod.send_notice,
        mod.buy_num,mod.price,(mod.buy_num*mod.price) tprice,mod.create_time,
        mod.bizpro_spec_value,mo.order_number';
        // 获取列表
        $data_params = [
            'page' => false,
            'where' => $_where,
            'field'=>$field,
            'mine'=>'mineOrder',
        ];

        $ret=OrderService::OrderList($data_params);
        $data = empty($ret['data']) ? [] : $ret['data'];

        if(!empty($data)){
            foreach($data as $k=>$v)
            {
                $data[$k]['bizpro_spec_value']=$v['bizpro_spec_value'];

            }
        }

        return json(apiDataReturn(true, '',$data));
    }
    /**
     * @param array
     * @content 我的页  点击确认收货
     */
    function getConfirmReceipt(){
        #接受订单详情id
        $odetail_id = input("odetail_id");
        if(!empty($odetail_id)){
            $res = Db::table("member_orderdetail")->where("id  = $odetail_id")->update(array('order_status'=>4));
            if($res){
                return apiDataReturn(true,'确认收货成功');
            }else{
                return apiDataReturn(false, '确认收货失败,请稍后重试');
            }
        }else{
            return apiDataReturn(true,'没有订单详情id');

        }
    }
    /**
     * @param array
     * @content 我的页  请求消费记录
     */
    function getMineSale(){
        #查订单
        $memberOrder =Db::table("log_pay")->field("order_id, create_time pay_time, total_price pay_price")->where("member_id = $this->member_id")->select();
        if(!empty($memberOrder)){
            return apiDataReturn(true,'',$memberOrder);
        }else{
            return apiDataReturn(false,'消费记录为空');
        }
    }
    /**
     * @param array
     * @content 检测是否更新
     */
    function testVersion()
    {
        $appid = $_GET["appid"];
        $version = $_GET["version"]; //客户端版本号
        $rsp = array("status" => 0); //默认返回值，不需要升级

        if (isset($appid) && isset($version)) {
            #查询版本数据
            $info = Db::table('edition')->order('id desc')->find();
            if ($appid === $info['appid']) { //校验appid
                if ($version !== $info['version']) { //这里是示例代码，真实业务上，最新版本号及relase notes可以存储在数据库或文件中
                    $rsp["status"] = 1;
                    $rsp["note"] = $info['note']; //release notes  更新说明
                    $rsp["url"] = $info['url']; //应用升级包下载地址
                    $rsp["update_time"] = $info['update_time']; //更新时间
                    $rsp["size"] = $info['size']; //更新包大小
                    $rsp["version"] = $info['version']; //更新包大小
                    $rsp["fore_status"] = $info['status']; //1强制 2 不强制
                }
            }

        }
        return apiDataReturn(true,'',$rsp);
    }
    /**
     * @param array
     * @content 前端报存图片
     */
    function shareImgurl(){
        #获取报存图片路径
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        $res = $http_type . $_SERVER['HTTP_HOST'] . "/upload/" . "shareimg/share-app.png";
        return array('status'=>true,'img'=>$res);
//
//
//            $filename = ROOT_PATH.'static/layuimini/images/logo.png';
//            header("Content-Type: application/force-download");
//            header('Content-Disposition: attachment; filename="logo.png"');
//            $img = file_get_contents($filename);
//            return array('status'=>true,'img'=>mb_convert_encoding($img, 'UTF-8', 'UTF-8'));
//            echo $img;
//            die;

    }


    /**
     * [GetOrderCount 获取订单数量].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function GetOrderCount()
    {
        MemberService::MemberStatusCheck('id', $this->member_id);
        #待付款
        $count_unpay=Db::name('member_order')->where([['member_id','=',$this->member_id],['order_status','=',1],['is_delete','=',2]])->count();
       #待发货
        $count_unsend=OrderService::DataTotal('',[['mo.member_id','=',$this->member_id],['mod.order_status','=',2],['mod.is_delete','=',2]]);
        #运输中
        $count_sending=OrderService::DataTotal('',[['mo.member_id','=',$this->member_id],['mod.order_status','=',3],['mod.is_delete','=',2]]);
        $re=['count_unpay'=>$count_unpay,'count_unsend'=>$count_unsend,'count_sending'=>$count_sending];

        return json(DataReturn('获取成功','0',$re));
    }






}